/*
 * Copyright (c) 2005, 2014 vacoor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.vacoor.nothing.demo.ui.snap;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;

/**
 * Created with IntelliJ IDEA.
 * User: vacoor
 * Date: 13-8-18
 * Time: 下午11:17
 * To change this template use File | Settings | File Templates.
 */
public class SnapFrame extends JFrame {
    private Canvas canvas;
    private Dialog dialog;
    private JFileChooser fileChooser;

    private Dimension screenSize;
    private BufferedImage screenImg;


    public SnapFrame() {
        init();
    }

    private void init() {
        try {
            screenSize = Toolkit.getDefaultToolkit().getScreenSize();

            // 截图操作比较耗时, 这里只在初始化时候获取一次
            screenImg = new Robot().createScreenCapture(new Rectangle(0, 0, screenSize.width, screenSize.height));
        } catch (AWTException e) {
            e.printStackTrace();
            System.exit(1);
        }

        canvas = new Canvas() {
            @Override
            public void paint(Graphics g) {
                super.paint(g);
                g.drawImage(screenImg, 0, 0, screenSize.width, screenSize.height, null);
            }
        };
        dialog = new OperationDialog(null);
        this.getContentPane().add(canvas);

        SnapCanvansListener listener = new SnapCanvansListener(screenImg, canvas, dialog);

        canvas.addMouseListener(listener);
        canvas.addMouseMotionListener(listener);

        // 绑定 ESC 退出
        KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventPostProcessor(new KeyEventPostProcessor() {
            @Override
            public boolean postProcessKeyEvent(KeyEvent e) {
                if (KeyEvent.VK_ESCAPE == e.getKeyCode()) {
                    System.exit(0);
                }
                return false;
            }
        });
    }

    public static void main(String[] args) {
        SnapFrame snapFrame = new SnapFrame();
        snapFrame.setTitle("title");
//        snapFrame.setLocationRelativeTo(null);
//        snapFrame.setSize(500, 500);
        snapFrame.setDefaultCloseOperation(3);
//        snapFrame.setUndecorated(true);
        snapFrame.setVisible(true);
        snapFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    }
}
